/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 叶勤勇
 */
public class TaobaokeItemAccessor {
    private EntityStore store;
    PrimaryIndex<Long, TaobaokeItemEntity> priIndex;
    SecondaryIndex<String, Long, TaobaokeItemEntity> secIndex;
    
    public TaobaokeItemAccessor(EntityStore store) throws DatabaseException {
        this.store = store;
        priIndex = store.getPrimaryIndex(Long.class, TaobaokeItemEntity.class);
        secIndex = store.getSecondaryIndex(priIndex, String.class, "nick");
    }
    
    public void putTaobaokeItemEntity(TaobaokeItemEntity item) throws DatabaseException {
        priIndex.put(item);
    }
    
    public TaobaokeItemEntity getTaobaokeItemEntity(Long numIid) throws DatabaseException {
        return priIndex.get(numIid);
    }
    
    public List<TaobaokeItemEntity> getTaobaokeItemEntities(String nick) throws DatabaseException {
        List<TaobaokeItemEntity> items = new ArrayList<TaobaokeItemEntity>();
        EntityCursor<TaobaokeItemEntity> cursor = null;
        try {
            cursor = secIndex.subIndex(nick).entities();
            for(TaobaokeItemEntity item : cursor) {
                items.add(item);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }
        return items;
    }
}
